home *** CD-ROM | disk | FTP | other *** search
/ Interplay's Learn to Program Basic (Review Copy) / Learn to Program Basic Review Copy (Interplay)(June 23, 1998).ISO / pc / ltpbasic / projects / ufobombr.bas < prev    next >
Encoding:
BASIC Source File  |  1998-03-26  |  6.5 KB  |  343 lines

  1.  
  2. Rem UFO Bomber!
  3. Rem By Richard
  4.  
  5. Rem Variable Setup
  6. atkang = 3.1415926 / 4.0
  7. oldatk = atkang
  8. xbase = 0
  9. ybase = 240
  10. atklngth = 30
  11. score = 0
  12. numallowed = 1
  13. numbullets = 0
  14. maxbullets = 3
  15. bulletspd = 10
  16. ufohere = 0
  17. ufospeed = 0
  18. ufodir = 0
  19. ufox = 0
  20. ufoy = 0
  21. bombon = 0
  22. bombx = 0
  23. bomby = 0
  24. bombyp = 0
  25. ufohit = 0
  26. endx = cos(atkang)*atklngth + xbase
  27. endy = ybase - sin(atkang)*atklngth
  28.  
  29. Rem Set a maximum speed (in frames per second)
  30. fps = 25
  31. loopSpeed = 1000/fps
  32.  
  33. Rem Intstructions
  34. Cls
  35. Print "- UFO Bomber -"
  36. Print
  37. Print "Your goal is to stop bombardment"
  38. Print "of Planet Earth.  Use the keys A and Z"
  39. Print "keys to aim your weapon, and the space"
  40. Print "bar to fire.  Good luck!"
  41. Print
  42. Input "<Press the Enter or Return key to go on>",a$
  43. Cls
  44.  
  45. Rem Load in the sprites
  46. ufospr = LoadSprite("ufo")
  47. HideSprite ufospr
  48. expspr = LoadSprite("Woozy01")
  49. HideSprite expspr
  50. SetSprite expspr range 4 to 10
  51. bombspr = LoadSprite("Woozy01")
  52. SetSprite bombspr range 0 to 3
  53. HideSprite bombspr
  54. hits = 0
  55.  
  56.  
  57. Rem Set up our arrays, and set up the bullets
  58. Dim bulleton(maxbullets)
  59. Dim bulletspr(maxbullets)
  60. Dim bulletx(maxbullets)
  61. Dim bullety(maxbullets)
  62. Dim bulletxp(maxbullets)
  63. Dim bulletyp(maxbullets)
  64. for t = 1 to maxbullets
  65. bulletspr(t) = LoadSprite("bullet")
  66. bulleton(t) = 0
  67. next t
  68.  
  69. Rem Screen Setup
  70. Cls
  71. Background "earth"
  72. SetDrawLayer(TEXT)
  73. Gosub UpdateCannon
  74.  
  75. Rem Main Loop!
  76. Rem Wait for keypresses, while updating the UFO, its bomb,
  77. Rem and flying bullets.
  78. while 1
  79. lastTick = Timer()
  80. Gosub UpdateUFO
  81. Gosub UpdateBullets
  82. if bombon then Gosub UpdateBomb
  83.  
  84. Rem Handle keypresses for aiming the cannon
  85. if keydown("A") then
  86. atkang = atkang + .1
  87. if atkang > 1.54 then atkang = 1.54
  88. Gosub UpdateCannon
  89. Goto SkipRest
  90. Endif
  91. if keydown("z") then
  92. atkang = atkang - .1
  93. if atkang < .1 then atkang = .1
  94. Gosub UpdateCannon
  95. Goto SkipRest
  96. endif
  97.  
  98. SkipRest:
  99. Rem Fire cannon?
  100. if keydown("space") then Gosub FireCannon
  101.  
  102. Rem Limit overall execution speed to reasonable maximum
  103. while Timer()-lastTick < loopSpeed
  104. wend
  105.  
  106. wend
  107. end
  108.  
  109. Rem If the user moves the turret, play a little sound and redraw it on the screen.
  110. Rem Note that the cannon uses geometry COSINE and SINE calculations to draw a line
  111. Rem pointing outwards at an arbritrary angle.
  112.  
  113. UpdateCannon:
  114. Sound "tank2"
  115.  
  116. Rem Erase Old cannon line
  117. Rem Since we're using the text layer, we can use a -1 color to erase
  118. Color -1
  119. Line xbase,ybase+1 to endx,endy+1
  120. Line xbase,ybase to endx,endy
  121.  
  122. Rem Draw New cannon line
  123. Color 5 'Uh..Black?
  124. endx = cos(atkang)*atklngth + xbase
  125. endy = ybase - sin(atkang)*atklngth
  126. Line xbase,ybase+1 to endx,endy+1
  127. Line xbase,ybase to endx,endy
  128. return
  129.  
  130. Rem This routine fires a bullet
  131. Rem We use COSINE and SINE again to send the bullet out across the screen at a
  132. Rem certain angle.  We do a lot of pre-calculation on the bullet, so that
  133. Rem the bullet update routine (which is executed very often ) has to do as
  134. Rem little as possible.
  135.  
  136. FireCannon:
  137. if numbullets >= numallowed then return
  138. numbullets = numbullets + 1
  139. for t = 1 to maxbullets
  140. if bulleton(t)=0 then
  141. Sound "biggun1"
  142. bulletx(t) = endx
  143. bullety(t) = endy
  144. bulletxp(t) = cos(atkang)*bulletspd
  145. bulletyp(t) = -sin(atkang)*bulletspd
  146. SetSprite bulletspr(t) to bulletxp(t),bulletyp(t)
  147. ShowSprite bulletspr(t)
  148. CycleSprite bulletspr(t)
  149. bulleton(t) = 1
  150. t = maxbullets
  151. endif
  152. next t
  153. return
  154.  
  155. Rem Update bullets
  156. UpdateBullets:
  157. for t = 1 to maxbullets
  158. if bulleton(t) then
  159. bulletx(t) = bulletx(t) + bulletxp(t)
  160. bullety(t) = bullety(t) + bulletyp(t)
  161. Rem Simulate a little gravity on the bullet
  162. bulletyp(t) = bulletyp(t) + .1
  163. Rem Is bullet still valid?
  164. if bulletx(t) > 320 or bullety(t) < 0 then
  165. bulleton(t) = 0
  166. HideSprite bulletspr(t)
  167. numbullets = numbullets - 1
  168. else
  169. SetSprite bulletspr(t) to bulletx(t),bullety(t)
  170.  
  171. Rem Now check for a collision with a bomb sprite
  172. if bombon then
  173. if SpriteHitSprite(bombspr,bulletspr(t)) then
  174. score = score + (bombyp * 200)
  175. Sound "boing1"
  176. bombon = 0
  177. HideSprite bombspr
  178. numbullets = numbullets - 1
  179. HideSprite bulletSpr(t)
  180. bulleton(t) = 0
  181. endif
  182. endif
  183. Rem if theres a ufo on the screen, check to see if we hit it and if so,
  184. Rem destroy it.
  185. if ufohere and ufohit = 0 then
  186. if SpriteHitSprite(ufospr,bulletspr(t)) then
  187. score = score + (ufospd * 20)
  188. Sound "exp_big"
  189. HideSprite bulletspr(t)
  190. bulleton(t) = 0
  191. numbullets = numbullets - 1
  192.  
  193. Rem Upgrade players capabilities
  194. hits = hits + 1
  195. if hits = 10 then
  196. bulletspd = 14
  197. endif
  198. if hits = 15 then
  199. numallowed = 2
  200. bulletspd = 16
  201. endif
  202. if hits = 20 then
  203. bulletspd = 18
  204. endif
  205. if hits = 25 then
  206. numallowed = 3
  207. endif
  208.  
  209. ufohit = 25
  210. HideSprite ufospr
  211. SetSprite expspr to ufox,ufoy
  212. CycleSprite expspr
  213. endif
  214. endif
  215. endif
  216. endif
  217. next t
  218. return
  219.  
  220. Rem Update ufo sprite
  221. UpdateUFO:
  222. if ufohere = 0 then 
  223. if random(1,1000) > 25 then
  224. return
  225. else
  226. Sound "whizby"
  227. ufohere = 1
  228. TextColor -1
  229. ufox = 0
  230. ufoy = random(20,160)
  231. ufospd = random(2,4) + (hits / 5)
  232. CycleSprite ufospr
  233. endif
  234. else
  235. if ufohit then
  236. ufohit = ufohit - 1
  237. if ufohit = 0 then
  238. ufohere = 0
  239. HideSprite expspr
  240. endif
  241. return
  242. endif
  243. endif
  244.  
  245. ufox = ufox + ufospd
  246. if ufox > 320 then
  247. HideSprite ufospr
  248. ufohere = 0
  249. endif
  250.  
  251. SetSprite ufospr to ufox,ufoy
  252.  
  253. if bombon = 0 and random(1,100) = 2 then
  254. if ufoy < 130 then
  255. if ufox > 20 and ufox < 300 then
  256. bombon = 1
  257. bombx = ufox + 20
  258. bomby = ufoy
  259. bombyp = (hits / 8) + random(2,4)
  260. endif
  261. endif
  262. endif
  263.  
  264. return
  265.  
  266. Rem Update the UFO's bomb
  267. UpdateBomb:
  268.  
  269. bomby = bomby + bombyp
  270. SetSprite bombspr to bombx,bomby
  271. if bomby > 230 then Goto GameOver
  272. return
  273.  
  274. Rem this is the end...
  275. Rem A variety of techniques are being used in this
  276. Rem "Game Over" scene.  One thing to notice is that
  277. Rem the 'x' position of the astronaut is also driving
  278. Rem other events.  Also, 'snd' is used as a counter
  279. Rem to trigger a sound effect every tenth iteration of the
  280. Rem loop.  Learning these methods can help you create
  281. Rem interesting scenes where a variety of things are
  282. Rem happening at once.
  283.  
  284. GameOver:
  285. cls
  286. HideSprite ufospr
  287. HideSprite expspr
  288. for t = 1 to maxbullets
  289. HideSprite bulletspr(t)
  290. next t
  291.  
  292. Background "earth"
  293. astro = LoadSprite("astro")
  294. x = -40
  295. TextColor 6
  296.  
  297. snd = 0
  298. while x < 350
  299. x =x + 1
  300. SetSprite astro to x,80
  301. Rem part 1 ... a beam of devastation from above
  302. if x > 80 and x < 100 then
  303.  
  304. if snd > 10 then
  305. snd = 0
  306. sound "lazer1"
  307. else
  308. snd = snd + 1
  309. endif
  310.  
  311. diff = x - 80
  312. if diff > 11 then diff = 11
  313. color 6 + x
  314. line 160 - diff, 1 to 160 - diff,240
  315. line 160 + diff, 1 to 160 + diff,240
  316. endif
  317.  
  318. Rem part 2 ... effect of said beam
  319. if x > 100 then
  320.  
  321. if snd > 10 then
  322. snd = 0
  323. sound "exp_far"
  324. else
  325. snd = snd + 1
  326. endif
  327.  
  328. diff = x - 100
  329. xcircle = random(1,320)
  330. ycircle = 240 - (diff*2)
  331. color random(6,21)
  332. FillCircle xcircle,ycircle,40-diff
  333. endif
  334. wend
  335. Color 21
  336. FillRect 0,0 to 320,240
  337. DrawText "@RED,HELVETICA,BOLD,50@Game Over" AT 25,10
  338. Banner "The Earth Is Toast"
  339. End
  340.  
  341.  
  342.